package com.suse.service.admin.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suse.common.config.response.ApiException;
import com.suse.common.config.response.R;
import com.suse.service.admin.edu.mapper.ChapterMapper;
import com.suse.service.admin.edu.model.entity.Chapter;
import com.suse.service.admin.edu.model.entity.Video;
import com.suse.service.admin.edu.model.param.ChapterSaveParam;
import com.suse.service.admin.edu.model.param.ChapterUpdateParam;
import com.suse.service.admin.edu.model.vo.ChapterInfoVo;
import com.suse.service.admin.edu.model.vo.ChapterVo;
import com.suse.service.admin.edu.model.vo.VideoVo;
import com.suse.service.admin.edu.service.ChapterService;
import com.suse.service.admin.edu.service.VideoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-11
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Resource
    private VideoService videoService;

    @Override
    public R getChapterListByCourseId(String courseId) {
        // 获取章节列表
        List<Chapter> chapterList = lambdaQuery()
                .select(Chapter::getId,
                        Chapter::getTitle)
                .eq(Chapter::getCourseId, courseId)
                .orderByAsc(Chapter::getSort,Chapter::getGmtCreate)
                .list();
        // 封装为视图对象
        List<ChapterVo> chapterListVoList = new ArrayList<>();
        for (Chapter chapter : chapterList) {
            ChapterVo chapterVo = BeanUtil.copyProperties(chapter, ChapterVo.class);
            chapterListVoList.add(chapterVo);
            // 获取视频列表
            List<Video> videoList = videoService.lambdaQuery()
                    .select(Video::getId,
                            Video::getTitle,
                            Video::getIsFree,
                            Video::getVideoSourceId)
                    .eq(Video::getChapterId, chapter.getId())
                    .orderByAsc(Video::getSort,Video::getGmtCreate)
                    .list();
            // 封装为视图对象
            List<VideoVo> videoVoList = BeanUtil.copyToList(videoList, VideoVo.class);
            chapterVo.setVideoVoList(videoVoList);
        }
        return R.success(chapterListVoList);
    }

    @Override
    public R saveChapter(ChapterSaveParam chapterSaveParam) {
        Chapter chapter = BeanUtil.copyProperties(chapterSaveParam, Chapter.class);
        if (save(chapter)) {
            return R.success("添加成功");
        }else {
            throw new ApiException("添加失败！");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R updateChapter(ChapterUpdateParam chapterUpdateParam) {
        Chapter chapter = BeanUtil.copyProperties(chapterUpdateParam, Chapter.class);
        if (updateById(chapter)) {
            return R.success("修改成功");
        }else {
            throw new ApiException("修改失败！");
        }

    }

    @Override
    public R getChapterInfoById(String id) {
        Chapter chapter = lambdaQuery()
                .select(Chapter::getId,
                        Chapter::getTitle,
                        Chapter::getSort)
                .eq(Chapter::getId, id)
                .one();
        ChapterInfoVo chapterInfoVo = BeanUtil.copyProperties(chapter, ChapterInfoVo.class);
        return R.success(chapterInfoVo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R removeChapterById(String id) {

        // TODO 删除云视频

        // 删除章节视频
        removeChapterVideo(id);
        // 删除章节
        if (removeById(id)) {
            return R.success("删除成功");
        }else {
            throw new ApiException("删除失败！");
        }
    }

    /**
     * 删除章节视频
     * @param chapterId 章节ID
     * @return 是否成功
     */
    private boolean removeChapterVideo(String chapterId) {
        return videoService.lambdaUpdate()
                .eq(Video::getChapterId, chapterId)
                .remove();
    }


}
